Public Class CreditNotesList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents dgCreditNotesList As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblUserName As System.Web.UI.WebControls.Label
    Protected WithEvents removeCNID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents lblTotalNumber As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region


#Region " Properties"
    Protected ReadOnly Property UserID() As Integer
        Get
            Try
                If IsNothing(Request.QueryString("UserId")) Then
                    Return 0
                End If

                Dim id As Integer
                id = CInt(Request.QueryString("UserId"))

                If id < 0 Then
                    Throw New Exception
                End If

                Return id
            Catch ex As Exception
                Throw New ApplicationException("Invalid or missing parameter", ex)
            End Try
        End Get
    End Property

    Private Property Url() As String
        Get
            Return CStr(ViewState("Url"))
        End Get
        Set(ByVal Value As String)
            ViewState("Url") = Value
        End Set
    End Property

    Private Property UrlReferrer() As String
        Get
            Return CStr(ViewState("UrlReferrer"))
        End Get
        Set(ByVal Value As String)
            ViewState("UrlReferrer") = Value
        End Set
    End Property

    Private Property SortExpression() As String
        Get
            If (ViewState("SortExpression") Is Nothing) Then
                Return "NUMBER"
            Else
                Return CStr(ViewState("SortExpression"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortExpression") = Value
        End Set
    End Property

    Private Property SortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                Return " ASC"
            Else
                Return CStr(ViewState("SortDirection"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortDirection") = Value
        End Set
    End Property
#End Region

    Protected WithEvents Header1 As Header

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Me.IsPostBack Then
            Security.InternalAndAdminOnly()
            Me.Url = Request.Url.PathAndQuery
            Me.UrlReferrer = Request.UrlReferrer.PathAndQuery
            'Assign header
            Header1.NavigationBar.Add("Create new credit notes", AppRoot() & "/Customer/CreditNotes.aspx?UserId=" & Me.UserID.ToString())
            Header1.NavigationBar.Add("Refresh", Me.Url)
            Header1.NavigationBar.Add("Back", AppRoot() + "/Customer/CustomerList.aspx")

            Header1.MenuIndex = EMenuIndex.InternalUserList

            'Build datagrid                        
            If UserID <> 0 Then
                Dim dt As DataTable = CCreditNotes.GetCreditNotesList(UserID)
                Dim strUserName As String = CUser.GetName(UserID)
                lblUserName.Text = strUserName
                Session("CreditNotesList") = dt
                Me.dgCreditNotesList.DataSource = dt
                Me.dgCreditNotesList.DataBind()
                lblTotalNumber.Text = dt.Rows.Count.ToString()
            End If

        End If

    End Sub

    Private Sub dgCreditNotesList_SortCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgCreditNotesList.SortCommand
        ' Retrieve the data source from session state.
        Dim dt As DataTable = CType(Session("CreditNotesList"), DataTable)

        ' Create a DataView from the DataTable.
        Dim dv As DataView = New DataView(dt)

        ' The DataView provides an easy way to sort. Simply set the
        ' Sort property with the name of the field to sort by.
        If e.SortExpression <> Me.SortExpression Then
            dv.Sort = e.SortExpression & " ASC"
            Me.SortExpression = e.SortExpression
            Me.SortDirection = " DESC"
        Else
            dv.Sort = e.SortExpression & Me.SortDirection
            If (Me.SortDirection = " DESC") Then
                Me.SortDirection = " ASC"
            Else
                Me.SortDirection = " DESC"
            End If
        End If


        ' Rebind the data source and specify that it should be sorted
        ' by the field specified in the SortExpression property.
        dgCreditNotesList.DataSource = dv
        dgCreditNotesList.DataBind()
    End Sub

    Protected Sub RemoveCN_Command(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim creditID As Integer = CInt(removeCNID.Value)
        Dim creditnotesDS As New CCreditNotes
        creditnotesDS.Load(creditID)
        creditnotesDS.Remove()
        Response.Redirect(Me.Url)
    End Sub

End Class
